
package fenetres;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

import modeles.*;

import controles.*;


public class ListOperationFrame extends JInternalFrame{
JPanel entetePanel, listPanel;
	
	public JTable produitTable;
	public GenericTableModel tableModel;
	String [] titreTable = {"Codes","Libelle","Quantite"};
	
	public JButton validerBut,annulerBut;
	
	public Vector<InfoStock> listeInfoStock;
	
	public Utilisateur userConnected;
	public Accueil accueil;
	
	
	DefaultMutableTreeNode root;
	DefaultMutableTreeNode achatOp;
	DefaultMutableTreeNode entreOp;
	DefaultMutableTreeNode venteOp;
	TreeModel model;
	JTree tree;

	
	public ListOperationFrame (Utilisateur _userConnected,Accueil _accueil){
		super("Liste operations",
	              false, //resizable
	              true, //closable
	              false, //maximizable
	              false);//iconifiable
		
		userConnected = _userConnected;
		accueil = _accueil;
		
		listeInfoStock =  new Vector<InfoStock >();
		boolean accept = checkUserConnected();
		if(accept){
			setSize(700,500);
			entetePanel = new JPanel ();
			entetePanel.setBounds(0,0,700,100);
			this.getContentPane().setLayout(null);
			elaboreEnTeteListProduit ();
			this.getContentPane().add(entetePanel);
			elaboreTableauProduit();
			
			
			//recuperation de la taille de la fenetre
			setLocation(30, 100);
			this.getContentPane().add(listPanel);
			this.setTitle("Liste de tous les operations en stock");
			this.setVisible(true);
		}
	}
	
	public boolean checkUserConnected(){
		//userConnected = accueil.authent.getUserConnected();
		if( userConnected == null){
			JOptionPane.showMessageDialog(null, "Aucun utilisateur connecte!! \n Veuillez connecter svp :) ","Erreur",JOptionPane.ERROR_MESSAGE);
			this.dispose();
			return false;
		}
		else {
			return true;
		}
	}
	public void elaboreEnTeteListProduit(){
		JLabel enteteLab  = new JLabel ("Liste de tous les operations sur stocks ");
		enteteLab.setFont(new java.awt.Font("Century Schoolbook L", java.awt.Font.ITALIC, 24));
		enteteLab.setForeground(Color.BLUE);
		entetePanel.setLayout(null);
		entetePanel.add(enteteLab);
		enteteLab.setBounds(100,50,450,25);
		
	}

	public void elaboreTableauProduit (){
		listPanel = new JPanel ();
		listPanel.setBounds(0,100,700,350);
		
		
		updateListOperations();
		
		
	}
	//mise à jour des donnees du tableau
public void updateTableauProduit (){
	/*DBManager dbManager = new DBManager ();
	Stock  oStockRecup = new Stock ();
	
	listeInfoStock  = oStockRecup.getListStock(dbManager);
		
		if(listeInfoStock.size()>0)
		{
			//tableModel.removeAllRows();
			for(int i=0; i<listeInfoStock.size();i++){
				
				tableModel.addNewRow();

				//element de la forme idProduit;idVendeur;prixNegoc;qte;resultat
				Stock oStock = listeInfoStock.get(i);
						
				tableModel.setValueAt(oStock.getStock_code(), i, 0);
				tableModel.setValueAt(oStock.getStock_name(), i, 1);
				tableModel.setValueAt(oStock.getStock_date_creat(), i, 2);	
			}
		}
		tableModel.fireTableDataChanged();*/
	}

//mise à jour du fenetre graphique
public void updateListOperations(){
	listPanel.setLayout(null);
	//elaboration de l'arbre
	root = new DefaultMutableTreeNode("Operations stock");
	achatOp= new DefaultMutableTreeNode("Achats");
	entreOp= new DefaultMutableTreeNode("Entrees");
	venteOp= new DefaultMutableTreeNode("Ventes");
	
	//entreeOp
	InfoStock oInfoStock = new InfoStock ();
	DBManager dbManager = new DBManager ();
	Vector<String> listBE = oInfoStock.getListNumBe(dbManager);
	for (int i = 0 ; i < listBE.size () ; i++)
	{
                    
		entreOp.add(new DefaultMutableTreeNode(listBE.get(i)));
	}
	
	//achatOp
	achatOp.add(new DefaultMutableTreeNode("vide"));
	
	//venteOp
	venteOp.add(new DefaultMutableTreeNode("vide"));
	
	root.add(entreOp);
	root.add(achatOp);
	root.add(venteOp);
	
	model = new DefaultTreeModel(root);
	tree = new JTree(model);
	
	JScrollPane treeScroll = new JScrollPane(tree);
	
	treeScroll.setBounds(5,5,300, 250);
	
	
	//tableau
	 tableModel = new GenericTableModel(titreTable);
	 produitTable = new JTable ();
	 produitTable.setOpaque(false);
	 produitTable.setRowHeight(25);
	 produitTable.setGridColor(Color.darkGray);
	 produitTable.setShowGrid(true);

	 produitTable.setModel(tableModel);
	 updateTableauProduit ();
	
	
	 JScrollPane tabScroll = new JScrollPane(produitTable);
	 tabScroll.setBounds(350,5,300, 250);

	validerBut = new JButton("Fermer");
	// validerBut.addActionListener(new ChoiseStockAction(this));
	
	validerBut.setBounds(250,320,150,25);
	
	listPanel.add(treeScroll);
	listPanel.add(tabScroll);
	listPanel.add(validerBut);
	
}


}